package com.jnu.tijian.service;

import com.jnu.tijian.domain.Checkitem;
import com.jnu.tijian.domain.Setmeal;
import com.jnu.tijian.domain.Setmealdetailed;
import com.jnu.tijian.dto.ResponseObject;
import com.jnu.tijian.mapper.CheckitemMapper;
import com.jnu.tijian.mapper.SetmealMapper;
import com.jnu.tijian.mapper.SetmealdetailedMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    SetmealdetailedMapper setmealdetailedMapper;

    @Autowired
    CheckitemMapper checkitemMapper;

    // 根据性别获取套餐列表数据，同时把每个套餐关联的检查项目数据查询出来
    @Override
    public ResponseObject listSetmeal(Integer sex){
        // 1.根据性别查询所有对应的套餐数据
        List<Setmeal> setmealList = setmealMapper.selectBySex(sex);

        // 2.拿套餐编号查询有哪些检查项，再根据检查项编号查看检查项的详细信息
        for (int i = 0; i < setmealList.size(); i++){
            Setmeal setmeal = setmealList.get(i);

            List<Setmealdetailed> setmealdetailedList = setmealdetailedMapper.selectBySetmealId(setmeal.getSmId());

            // 定义一个容器，存放当前套餐对应的检查项集合
            List<Checkitem> checkitemList = new ArrayList<>();

            // 再根据检查项编号查询检查项的详细信息
            for (Setmealdetailed setmealdetailed : setmealdetailedList) {
                Integer ciId = setmealdetailed.getCiId();
                Checkitem checkitem = checkitemMapper.selectByPrimaryKey(ciId);
                checkitemList.add(checkitem);
            }

            setmeal.setCheckitemList(checkitemList);
        }

        return new ResponseObject(ResponseObject.SUCCESS_STATUS, ResponseObject.SUCCESS_DESC, setmealList);
    }

    @Override
    public ResponseObject loadSetmeal(Setmeal setmeal) {
        return new ResponseObject(ResponseObject.SUCCESS_STATUS, ResponseObject.SUCCESS_DESC, setmealMapper.selectByPrimaryKey(setmeal.getSmId()));
    }
}
